import { _decorator, CircleCollider2D, Prefab, RigidBody2D } from 'cc';
import { BaseEntity } from './BaseEntity';
import TweenManager from '../../common/TweenManager';
import { AttackComponent } from '../components/AttackComponent';
import { EntityVo } from '../vo/EntityVo';
import { WanderComponent } from '../components/WanderComponent';

const { ccclass, property } = _decorator;

@ccclass('Enemy')
export class Enemy extends BaseEntity {
    @property(Prefab) deadParticle: Prefab = null;
    @property(RigidBody2D) rig: RigidBody2D = null;
    @property(CircleCollider2D) collider: CircleCollider2D = null;
    
    private _isDead: boolean = false;
    private _attackComponent: AttackComponent = null;
    private _wanderComponent: WanderComponent = null;

    protected onLoad(): void {
        super.onLoad();
        // 添加攻击组件
        this._attackComponent = this.addCustomComponent(AttackComponent);
        this._wanderComponent = this.addCustomComponent(WanderComponent);
    }

    public init() {
        this.node.active = true;
        this.rig.enabled = true;
        this.collider.enabled = true;
        this.rig.wakeUp();
        this._isDead = false;
        const vo = new EntityVo();
        this.setData(vo);
        // // 基础浮动动画
        // const originalY = this.node.position.y;
        // TweenManager.addTween(this.node, { loop: true }).to({y: originalY + 10 }, 500).to({y: originalY - 10}, 500);
    }

    update(deltaTime: number) {
        if (this._isDead) {
            this.reset();
        }
    }

    public death() {
        this._isDead = true;
    }

    public get isDead(): boolean {
        return this._isDead;
    }

    public reset(isOver: boolean = false) {
        TweenManager.removeTweens(this.node);
        this.rig.sleep();
        this.rig.enabled = false;
        this.collider.enabled = false;
        this.node.active = false;
        super.reset();
    }
}


